package phi.engine.actors 
{
	import Box2D.Collision.b2AABB;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Dynamics.b2Body;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import phi.engine.PhiEngine;
	import phi.engine.core.GameTime;
	import phi.engine.draw.IDrawableSprite;
	import phi.engine.events.ActorEvent;
	import phi.engine.layers.ILayer;
	import phi.engine.utils.PhiMathUtil;
	
	/**
	 * ...
	 * @author Alexandru Ghiura
	 */
	public class Actor extends Sprite implements IActor
	{
		// Public vars
		
		// Protected vars
		protected var engine :PhiEngine = PhiEngine.getInstance();
		protected var options :ActorOptions;
		
		// Private vars
		private var _data :Object = {};
		private var _position :Point;
		private var _layer :ILayer;
		
		/**
		 * Create an actor with some specific options.
		 * 
		 * @param	options
		 */
		public function Actor( options :ActorOptions=null ) 
		{
			// Listners
			addEventListener( ActorEvent.OPTIONS_CHANGE, optionsChangeHandler );
			
			// Change actor options
			if( options )
				this.options = options;
		}

		public function get layer():ILayer
		{
			return _layer;
		}

		public function set layer(value:ILayer):void
		{
			_layer = value;
		}

		public function get data():Object
		{
			return _data;
		}

		public function set data(value:Object):void
		{
			_data = value;
		}

		public function changeOptions( newOptions:ActorOptions ):void
		{
			// Save options
			options = newOptions;
			dispatchEvent( new ActorEvent( ActorEvent.OPTIONS_CHANGE, newOptions ));			
		}

		/*public function get position():Point
		{
			if( body == null )
				return new Point( x, y );
			
			return new Point( PhiMathUtil.metersToPixels( body.GetPosition().x ), PhiMathUtil.metersToPixels( body.GetPosition().y ));
		}
		
		public function get size():Point
		{
			if( body == null )
				return new Point( width, height );
				
			var extend :b2Vec2 = body.GetFixtureList().GetAABB().GetExtents(); 
			return new Point( PhiMathUtil.metersToPixels( extend.x * 2) , PhiMathUtil.metersToPixels( extend.y * 2) );
		}*/

		public function update( gameTime:GameTime ):void
		{
			draw( gameTime );
		}
		
		public function draw( gameTime:GameTime ):void
		{
		}
		
		public function handleInput():void
		{
		}
		
		public function destroy():void
		{
			if( layer != null )
				layer.removeActor( this );
		}
		
		// -----------------------------------------------------------------------------
		// Protected funcitons
		// -----------------------------------------------------------------------------
		
		protected function optionsChangeHandler( event:ActorEvent ):void
		{
		}
	}

}